<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="用户昵称" prop="userName">
            <el-input v-model="queryParams.userName" placeholder="请输入用户昵称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
<!--          <el-form-item label="提现单号" prop="withdrawNo">-->
<!--            <el-input v-model="queryParams.withdrawNo" placeholder="请输入提现单号" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="微信交易流水号" prop="transactionNo">-->
<!--            <el-input v-model="queryParams.transactionNo" placeholder="请输入微信交易流水号" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="交易金额" prop="amount">-->
<!--            <el-input v-model="queryParams.amount" placeholder="请输入交易金额" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="交易成功时间" prop="withdrawTime">-->
<!--            <el-date-picker clearable-->
<!--              v-model="queryParams.withdrawTime"-->
<!--              type="date"-->
<!--              value-format="YYYY-MM-DD"-->
<!--              placeholder="请选择交易成功时间"-->
<!--            />-->
<!--          </el-form-item>-->
          <el-form-item label="状态" prop="withdrawStatus">
            <el-select v-model="queryParams.withdrawStatus" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in withdraw_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="提现类型" prop="withdrawType">
          <el-select v-model="queryParams.withdrawType" placeholder="请选择提现类型" clearable>
            <el-option
              v-for="dict in withdraw_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          </el-form-item>

<!--          <el-form-item label="退款备注" prop="withdrawRemark">-->
<!--            <el-input v-model="queryParams.withdrawRemark" placeholder="请输入退款备注" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['device:withdraw:add']">新增</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['device:withdraw:edit']">修改</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['device:withdraw:remove']">删除</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['device:withdraw:export']">导出</el-button>-->
<!--          </el-col>-->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="withdrawList" border="true">
        <el-table-column type="index" width="55" align="center" fixed="left"/>
        <el-table-column label="提现ID" align="center" prop="withdrawId" v-if="false" />
        <el-table-column label="订单号" align="center" prop="orderNo" width="200" />
        <el-table-column label="提现类型" align="center" prop="withdrawType">
          <template #default="scope">
            <dict-tag :options="withdraw_type" :value="scope.row.withdrawType"/>
          </template>
        </el-table-column>

        <el-table-column label="用户昵称" align="center" prop="userName" />
        <el-table-column label="提现单号" align="center" prop="withdrawNo" width="200"/>
        <el-table-column label="微信退款交易流水号" align="center" prop="transactionNo" width="250"/>
        <el-table-column label="交易金额" align="center" prop="amount" />
        <el-table-column label="交易成功时间" align="center" prop="withdrawTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.withdrawTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="withdrawStatus">
          <template #default="scope">
            <dict-tag :options="withdraw_status" :value="scope.row.withdrawStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="退款备注" align="center" prop="withdrawRemark" />
<!--        <el-table-column label="备注" align="center" prop="remark" />-->
<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--          <template #default="scope">-->
<!--            <el-tooltip content="修改" placement="top">-->
<!--              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['device:withdraw:edit']"></el-button>-->
<!--            </el-tooltip>-->
<!--            <el-tooltip content="删除" placement="top">-->
<!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['device:withdraw:remove']"></el-button>-->
<!--            </el-tooltip>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改提现对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="withdrawFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="提现单号" prop="withdrawNo">
          <el-input v-model="form.withdrawNo" placeholder="请输入提现单号" />
        </el-form-item>
        <el-form-item label="微信交易流水号" prop="transactionNo">
          <el-input v-model="form.transactionNo" placeholder="请输入微信交易流水号" />
        </el-form-item>
        <el-form-item label="交易金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入交易金额" />
        </el-form-item>
        <el-form-item label="交易成功时间" prop="withdrawTime">
          <el-date-picker clearable
            v-model="form.withdrawTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择交易成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="withdrawStatus">
          <el-select v-model="form.withdrawStatus" placeholder="请选择状态">
            <el-option
                v-for="dict in withdraw_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款备注" prop="withdrawRemark">
          <el-input v-model="form.withdrawRemark" placeholder="请输入退款备注" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Withdraw" lang="ts">
import { listWithdraw, getWithdraw, delWithdraw, addWithdraw, updateWithdraw } from '@/api/device/withdraw';
import { WithdrawVO, WithdrawQuery, WithdrawForm } from '@/api/device/withdraw/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { withdraw_status,withdraw_type } = toRefs<any>(proxy?.useDict('withdraw_status','withdraw_type'));

const withdrawList = ref<WithdrawVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const withdrawFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: WithdrawForm = {
  withdrawId: undefined,
  orderNo: undefined,
  userName: undefined,
  withdrawNo: undefined,
  transactionNo: undefined,
  amount: undefined,
  withdrawTime: undefined,
  withdrawStatus: undefined,
  withdrawRemark: undefined,
  withdrawType: undefined,
  remark: undefined
}
const data = reactive<PageData<WithdrawForm, WithdrawQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 'create_time',
    isAsc: 'desc',
    orderNo: undefined,
    userName: undefined,
    withdrawNo: undefined,
    transactionNo: undefined,
    amount: undefined,
    withdrawTime: undefined,
    withdrawStatus: undefined,
    withdrawRemark: undefined,
    withdrawType: undefined,
    params: {
    }
  },
  rules: {
    withdrawId: [
      { required: true, message: "提现ID不能为空", trigger: "blur" }
    ],
    orderNo: [
      { required: true, message: "订单号不能为空", trigger: "blur" }
    ],
    userName: [
      { required: true, message: "用户昵称不能为空", trigger: "blur" }
    ],
    withdrawNo: [
      { required: true, message: "提现单号不能为空", trigger: "blur" }
    ],
    transactionNo: [
      { required: true, message: "微信交易流水号不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "交易金额不能为空", trigger: "blur" }
    ],
    withdrawTime: [
      { required: true, message: "交易成功时间不能为空", trigger: "blur" }
    ],
    withdrawStatus: [
      { required: true, message: "状态：withdrawing 提现中,withdraw_success 退款成功,fail 用户取消,交易失败(),不能为空", trigger: "change" }
    ],
    withdrawRemark: [
      { required: true, message: "退款备注不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询提现列表 */
const getList = async () => {
  loading.value = true;
  const res = await listWithdraw(queryParams.value);
  withdrawList.value = res.data.records;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  withdrawFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: WithdrawVO[]) => {
  ids.value = selection.map(item => item.withdrawId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加提现";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: WithdrawVO) => {
  reset();
  const _withdrawId = row?.withdrawId || ids.value[0]
  const res = await getWithdraw(_withdrawId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改提现";
}

/** 提交按钮 */
const submitForm = () => {
  withdrawFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.withdrawId) {
        await updateWithdraw(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addWithdraw(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: WithdrawVO) => {
  const _withdrawIds = row?.withdrawId || ids.value;
  await proxy?.$modal.confirm('是否确认删除提现编号为"' + _withdrawIds + '"的数据项？').finally(() => loading.value = false);
  await delWithdraw(_withdrawIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('device/withdraw/export', {
    ...queryParams.value
  }, `withdraw_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
